﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string _operator;
        double a;
        Boolean a1 = false;//判断a是否存在
        double b;
        Boolean b1 = false;//判断b是否存在


        private void button1_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "1";
            textBox1.Text += "1";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            _operator = "*";
            if (textBox2.Text == "")
            {

            }
            else
            {
                textBox1.Text += "×";
                a = double.Parse(textBox2.Text);
                a1 = true;
                textBox2.Text = "";
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "2";
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "3";
            textBox1.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "4";
            textBox1.Text += "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
           
            textBox2.Text += "5";
            textBox1.Text += "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "6";
            textBox1.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "7";
            textBox1.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
           
            textBox2.Text += "8";
            textBox1.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "9";
            textBox1.Text += "9";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += "0";
            textBox1.Text += "0";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            _operator = "+";
            if (textBox2.Text == "")
            {


            }
            else
            {
                textBox1.Text += "+";
                a = double.Parse(textBox2.Text);
                a1 = true;
                textBox2.Text = "";
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            _operator = "-";
            if (textBox2.Text == "")
            {

            }
            else
            {
                textBox1.Text += "-";
                a = double.Parse(textBox2.Text);
                a1 = true;
                textBox2.Text = "";
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            _operator = "/";
            
            if (textBox2.Text == "")
            {

            }
            else
            {
                textBox1.Text += "÷";
                a = double.Parse(textBox2.Text);
                a1 = true;
                textBox2.Text = "";
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (a1 == true && textBox2.Text != "")
            {
                b = double.Parse(textBox2.Text);
                b1 = true;
                if (_operator == "+")
                {
                    textBox1.Text = (a + "+" + b + "=");
                    string result = (a + b).ToString();
                    textBox1.Text += result;
                    textBox2.Text = "";
                }
                if (_operator == "-")
                {
                    textBox1.Text = (a + "-" + b + "=");
                    string result = (a - b).ToString();
                    textBox1.Text += result;
                    textBox2.Text = "";
                }
                if (_operator == "*")
                {
                    textBox1.Text = (a + "×" + b + "=");
                    string result = (a * b).ToString();
                    textBox1.Text += result;
                    textBox2.Text = "";
                }

                /*if (_operator == "/")
                {
                        textBox1.Text = (a + "÷" + b + "=");
                        double c;
                    c = (double)a;
                    try
                    {
                        double f = c / b;
                        string result = (f).ToString();
                        textBox1.Text += result;
                        textBox2.Text = "";
                    }catch(DivideByZeroException de)
                    {
                        textBox1.Text += "除数不能为0";
                    }  
                }*/

                if (_operator == "/")
                {
                    if (b != 0)
                    {
                        textBox1.Text = (a + "÷" + b + "=");
                        double f = a / b;
                        string result = (f).ToString();
                        textBox1.Text += result;
                        textBox2.Text = "";
                    }
                    if (b == 0)
                    {
                        textBox1.Text = "除数不能为0";
                        textBox2.Text = "";
                    }
                }
            }
            else { }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox2.Text += ".";
            textBox1.Text += ".";
        }
    }
}
